﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.SqlClient;
using Gestion_Api.AccesoDatos;
using Escuela.Forms;

namespace Escuela
{
    public partial class Cursos : Form
    {
        private int acep = 0;
        private Instituto inst;
        
        public Cursos(Instituto inst)
        {
            this.inst = inst;
            InitializeComponent();
        }

        private void Cursos_Load(object sender, EventArgs e)
        {
           
            try
            {

                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_seminario_GetAll' table. You can move, or remove it, as needed.
                this.coop_seminario_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_seminario_GetAll);
                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_curso_GetAll' table. You can move, or remove it, as needed.
                this.coop_curso_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_curso_GetAll);
                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_profesor_GetAll' table. You can move, or remove it, as needed.
                this.coop_profesor_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_profesor_GetAll);
                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_materia_GetAll' table. You can move, or remove it, as needed.
                this.coop_materia_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_materia_GetAll);
                this.enlazarDataGridCursos();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error cargando formulario " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void cargarSeminariosGrid()
        {
            try
            {

            }
            catch
            {

            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            //MateriaForm tCform = new MateriaForm();
            //tCform.Show(this);
        }

        

        

        private void btCancelar_Click(object sender, EventArgs e)
        {
            enableBtn();
            //clearTxt();
            disableTxt();
            //cursosBindingSource.RemoveFilter();
            
        }

        private void Nuevo_Click(object sender, EventArgs e)
        {
            this.acep = 1;
            this.maskCosto.Text = "0";
            this.maskedTextBox2.Text = "0";
            clearTxt();
            disableBtn();
            enableTxt();
            if (this.radioButton2.Checked) {
                this.maskedTextBox2.Enabled = false;
            }
        }

        private void Editar_Click(object sender, EventArgs e)
        {
            if (this.maskedNroCur.Text.Trim() != "")
            {
                this.acep = 2;
                enableTxt();
                disableBtn();
                this.dateTimePicker2.Enabled = false;
                this.dateTimePicker1.Enabled = false;
                this.maskedTextBox2.Enabled = false;
                this.textObs.Enabled = false;
                this.comboNom.Enabled = false;
                this.comboProf.Enabled = false;
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Curso/Seminario", "Advertencia");
            }
        }

        private void Borrar_Click(object sender, EventArgs e)
        {
            if (this.maskedNroCur.Text.Trim() != "")
            {
                this.acep = 3;
                disableBtn();
            }
            else
            {
                MessageBox.Show("Debe seleccionar un alumno", "Advertencia");
            }
        }

        private void Buscar_Click(object sender, EventArgs e)
        {
            //acep = 4;
            //enableTxt();
            //this.maskedNroCur.Enabled = true;
            //disableBtn();
        }

        private void Aceptar_Click(object sender, EventArgs e)
        {

            switch (acep)
            {
                #region nuevo
                case 1:
                    try
                    {
                        //Cursos
                        if (radioButton1.Checked)
                        {
                            this.nuevoCurso();
                        }
                        //Seminario
                        else
                        {
                            
                            this.nuevoSeminario();
                        }


                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error al agregar curso/Seminario: " + ex);
                    }
                    break;
                #endregion

                #region editar
                case 2:
                    try
                    {
                        //Cursos
                        if (radioButton1.Checked)
                        {
                            Curso cu = new Curso();
                            cu.Id = Convert.ToInt32(this.maskedNroCur.Text.Trim());
                            //int idMateria = Convert.ToInt32(this.comboNom.SelectedValue);
                            string dniProfesor = this.comboProf.SelectedValue.ToString().Trim();

                            //cu.FechaInicio = Convert.ToDateTime(this.dateTimePicker1.Text);
                            //cu.FechaFin = Convert.ToDateTime(this.dateTimePicker2.Text);
                            cu.HoraInicio = this.maskInicio.Text.Trim();
                            cu.HoraFin = this.maskFin.Text.Trim();
                            cu.Dia = this.comboDia.Text.Trim();
                            //cu.Observacion = this.textObs.Text.Trim();
                            cu.Costo = Convert.ToDecimal(this.maskCosto.Text.Trim());
                            cu.Cupos = Convert.ToInt32(this.maskCupo.Text);
                            //cu.CuposLibres = Convert.ToInt32(this.maskCupLib.Text);
                            cu.CuposLibres = 10;
                            cu.Aula = this.comboAula.Text;
                            //cu.Matricula = Convert.ToDecimal(this.maskedTextBox2.Text.Trim());

                            if (this.inst.modificarCursable(cu))
                            {
                                MessageBox.Show("Curso modificado con Exito", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_seminario_GetAll' table. You can move, or remove it, as needed.
                                this.coop_seminario_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_seminario_GetAll);
                                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_curso_GetAll' table. You can move, or remove it, as needed.
                                this.coop_curso_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_curso_GetAll);
                                this.disableTxt();
                            }
                            else
                            {
                                MessageBox.Show("Error al modificar curso verifique los datos ingresados", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        //Seminario
                        else
                        {
                            Seminario se = new Seminario();
                            se.Id = Convert.ToInt32(this.maskedNroCur.Text.Trim());
                            //int idMateria = Convert.ToInt32(this.comboNom.SelectedValue);
                            string dniProfesor = this.comboProf.SelectedValue.ToString();

                            //se.FechaInicio = Convert.ToDateTime(this.dateTimePicker1.Text);
                            //se.FechaFin = Convert.ToDateTime(this.dateTimePicker2.Text);
                            se.HoraInicio = this.maskInicio.Text.Trim();
                            se.HoraFin = this.maskFin.Text.Trim();
                            se.Dia = this.comboDia.Text.Trim();
                            //se.Observacion = this.textObs.Text.Trim();
                            se.Costo = Convert.ToDecimal(this.maskCosto.Text.Trim());
                            se.Cupos = Convert.ToInt32(this.maskCupo.Text);
                            //se.CuposLibres = Convert.ToInt32(this.maskCupLib.Text);
                            se.CuposLibres = 10;
                            se.Aula = this.comboAula.Text;
                            //se.Matricula = Convert.ToDecimal(this.maskedTextBox2.Text.Trim());

                            if (this.inst.modificarCursable(se))
                            {
                                MessageBox.Show("Seminario modificado con Exito", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_seminario_GetAll' table. You can move, or remove it, as needed.
                                this.coop_seminario_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_seminario_GetAll);
                                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_curso_GetAll' table. You can move, or remove it, as needed.
                                this.coop_curso_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_curso_GetAll);
                                this.disableTxt();

                            }
                            else
                            {
                                MessageBox.Show("Error al modificar seminario verifique los datos ingresados", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 
                            }

                        }


                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error al agregar curso/Seminario: " + ex);
                    }
                    break;

                    
                #endregion
                
                #region borrar
                case 3:


                    try
                    {
                        int nrocursable = Convert.ToInt32(this.maskedNroCur.Text.Trim());
                        //Cursos
                        if (radioButton1.Checked)
                        {
                            int i = this.inst.darDeBajaCursable(nrocursable);
                            if (i == 1)
                            {
                                MessageBox.Show("Curso eliminado con Exito");

                                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_seminario_GetAll' table. You can move, or remove it, as needed.
                                this.coop_seminario_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_seminario_GetAll);
                                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_curso_GetAll' table. You can move, or remove it, as needed.
                                this.coop_curso_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_curso_GetAll);
                                this.disableTxt();
                            }
                            if (i == 0)
                            {
                                MessageBox.Show("Error al eliminar Curso. Verifique los datos ingresados");
                            }
                            if (i == -1) 
                            {
                                MessageBox.Show("El curso tiene alumnos inscriptos, debe eliminaros antes de continuar");
                            }
                        }
                        //Seminario
                        else
                        {
                            int i = this.inst.darDeBajaCursable(nrocursable);
                            if (i == 1)
                            {
                                MessageBox.Show("Seminario eliminado con Exito");

                                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_seminario_GetAll' table. You can move, or remove it, as needed.
                                this.coop_seminario_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_seminario_GetAll);
                                // TODO: This line of code loads data into the 'escuelaDataSet10.coop_curso_GetAll' table. You can move, or remove it, as needed.
                                this.coop_curso_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_curso_GetAll);
                                this.disableTxt();

                            }
                            if (i == 0)
                            {
                                MessageBox.Show("Error al eliminar Seminario. Verifique los datos ingresados");
                            }
                            if (i == -1)
                            {
                                MessageBox.Show("El seminario tiene alumnos inscriptos, debe eliminaros antes de continuar");
                            }

                        }


                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error al eliminar Curso/Seminario: " + ex);
                    }
                    break;
            //    case 3:
            //        try
            //        {
            //            Curso cu = new Curso();
            //            int nroCurso = Convert.ToInt32(this.maskedNroCur.Text.Trim());
            //            int verif = 0;
            //            Confirmacion cf = new Confirmacion();
            //            cf.label1.Text = "Confirma Eliminar este curso? ";
            //            //cf.ShowDialog(this);
            //            if (cf.ShowDialog(this) == DialogResult.OK)
            //            {
            //                verif = cu.borrarCurso(nroCurso);

            //                if (verif != 0)
            //                {
            //                    MessageBox.Show("Curso borrado con Exito");

            //                    //Actualizo el datagridview
            //                    this.cursosTableAdapter.Fill(this.escuelaDataSet5.cursos);
            //                }
            //                else
            //                {
            //                    MessageBox.Show("Error al borrar Curso");
            //                }

            //            }
            //            else
            //            {
            //                //this.txtResult.Text = "Cancelled";
            //            }
            //            cf.Dispose();


            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show("Se produjo una excepcion a borrar alumno" + ex.Message);
            //        }

                #endregion
            }
        }

        //nuevo curso
        public void nuevoCurso()
        {
            try
            {
                Curso cu = new Curso();
                int idMateria = Convert.ToInt32(this.comboNom.SelectedValue);
                string dniProfesor = this.comboProf.SelectedValue.ToString().Trim();

                cu.FechaInicio = Convert.ToDateTime(this.dateTimePicker1.Text);
                cu.FechaFin = Convert.ToDateTime(this.dateTimePicker2.Text);
                cu.HoraInicio = this.maskInicio.Text.Trim();
                cu.HoraFin = this.maskFin.Text.Trim();
                cu.Dia = this.comboDia.Text.Trim();
                cu.Observacion = this.textObs.Text.Trim();
                cu.Costo = Convert.ToDecimal(this.maskCosto.Text.Trim());
                cu.Cupos = Convert.ToInt32(this.maskCupo.Text);
                //cu.CuposLibres = Convert.ToInt32(this.maskCupLib.Text);
                cu.CuposLibres = 10;
                cu.Aula = this.comboAula.Text;
                cu.Matricula = Convert.ToDecimal(this.maskedTextBox2.Text.Trim());

                if (this.inst.darDeAltaCurso(cu, idMateria, dniProfesor))
                {
                    MessageBox.Show("Curso agregado con Exito", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    // TODO: This line of code loads data into the 'escuelaDataSet10.coop_seminario_GetAll' table. You can move, or remove it, as needed.
                    this.coop_seminario_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_seminario_GetAll);
                    // TODO: This line of code loads data into the 'escuelaDataSet10.coop_curso_GetAll' table. You can move, or remove it, as needed.
                    this.coop_curso_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_curso_GetAll);
                    this.disableTxt();
                }
                else
                {
                    MessageBox.Show("Error al agregar curso verifique los datos ingresados", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al generar nuevo Curso." + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void nuevoSeminario()
        {
            try
            {
                Seminario se = new Seminario();
                int idMateria = Convert.ToInt32(this.comboNom.SelectedValue);
                string dniProfesor = this.comboProf.SelectedValue.ToString().Trim();

                se.FechaInicio = Convert.ToDateTime(this.dateTimePicker1.Text);
                se.FechaFin = Convert.ToDateTime(this.dateTimePicker2.Text);
                se.HoraInicio = this.maskInicio.Text.Trim();
                se.HoraFin = this.maskFin.Text.Trim();
                se.Dia = this.comboDia.Text.Trim();
                se.Observacion = this.textObs.Text.Trim();
                se.Costo = Convert.ToDecimal(this.maskCosto.Text.Trim());
                se.Cupos = Convert.ToInt32(this.maskCupo.Text);
                //se.CuposLibres = Convert.ToInt32(this.maskCupLib.Text);
                se.CuposLibres = 10;
                se.Aula = this.comboAula.Text;
                //se.Matricula = Convert.ToDecimal(this.maskedTextBox2.Text.Trim());

                if (this.inst.darDeAltaSeminario(se, idMateria, dniProfesor))
                {
                    MessageBox.Show("Seminario agregado con Exito", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    // TODO: This line of code loads data into the 'escuelaDataSet10.coop_seminario_GetAll' table. You can move, or remove it, as needed.
                    this.coop_seminario_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_seminario_GetAll);
                    // TODO: This line of code loads data into the 'escuelaDataSet10.coop_curso_GetAll' table. You can move, or remove it, as needed.
                    this.coop_curso_GetAllTableAdapter.Fill(this.escuelaDataSet10.coop_curso_GetAll);
                    this.disableTxt();

                }
                else
                {
                    MessageBox.Show("Error al agregar seminario verifique los datos ingresados", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al generar nuevo seminario." + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        protected void disableBtn()
        {
            this.Aceptar.Enabled = true;
            this.Cancelar.Enabled = true;
            this.Nuevo.Enabled = false;
            this.Editar.Enabled = false;
            this.Borrar.Enabled = false;
            this.Buscar.Enabled = false;
            this.verClasesbutton2.Enabled = false;
            //this.button4.Enabled = false;
            this.btnAgregAlum.Enabled = false;
            this.Cerrar.Enabled = false;
        }

        protected void enableBtn()
        {
            this.Aceptar.Enabled = false;
            this.Cancelar.Enabled = false;
            this.Nuevo.Enabled = true;
            this.Editar.Enabled = true;
            this.Borrar.Enabled = true;
            this.Buscar.Enabled = true;
            //this.button1.Enabled = true;
            //this.button4.Enabled = true;
            this.verClasesbutton2.Enabled = true;
            this.btnAgregAlum.Enabled = true;
            this.Cerrar.Enabled = true;
        }

        protected void clearTxt()
        {
            this.maskedNroCur.Clear();
            this.comboNom.Text = "";
            this.maskCupo.Clear();
            //this.maskCupLib.Clear();
            this.comboAula.ResetText();
            this.comboProf.ResetText();
            this.dateTimePicker1.ResetText();
            this.dateTimePicker2.ResetText();
            this.maskInicio.ResetText();
            this.maskFin.ResetText();
            this.comboDia.ResetText();
            this.textObs.Clear();
        }

        protected void enableTxt()
        {
            this.comboNom.Enabled=true;
            this.maskCupo.Enabled = true;
            //this.maskCupLib.Enabled = true;
            this.comboAula.Enabled = true;
            this.comboProf.Enabled = true;
            this.dateTimePicker1.Enabled = true;
            this.dateTimePicker2.Enabled = true;
            this.maskInicio.Enabled = true;
            this.maskFin.Enabled = true;
            this.comboDia.Enabled = true;
            this.textObs.Enabled = true;
            this.maskCosto.Enabled = true;
            this.maskedTextBox2.Enabled = true;
        }

        protected void disableTxt()
        {
            this.maskedNroCur.Enabled = false;
            this.comboNom.Enabled = false;
            this.maskCupo.Enabled = false;
            //this.maskCupLib.Enabled = false;
            this.comboAula.Enabled = false;
            this.comboProf.Enabled = false;
            this.dateTimePicker1.Enabled = false;
            this.dateTimePicker2.Enabled = false;
            this.maskInicio.Enabled = false;
            this.maskFin.Enabled = false;
            this.comboDia.Enabled = false;
            this.textObs.Enabled = false;
            this.maskCosto.Enabled = false;
            this.maskedTextBox2.Enabled = false;
        }

        //private void cargarProfesor()
        //{
        //    SqlDataReader dr = null;
        //    try
        //    {
        //        AccesoDB Ac = new AccesoDB();
        //        string query = "select idProfesores, nombre, apellido from profesores ";
        //        dr = Ac.execDR(query);

        //        while (dr.Read())
        //        {
        //            this.comboProf.Items.Add(dr[0].ToString() + "-" + dr[1].ToString() + " " + dr[2].ToString());
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("Error al cargar combo de Profesores" + ex.Message);
        //    }
        //    finally
        //    {
        //        dr.Close();
        //    }
        //}

        //private string verProfesor(int idProfesor)
        //{
        //    SqlDataReader dr = null;
        //    string profesor=null;
        //    try
        //    {
        //        AccesoDB Ac = new AccesoDB();
        //        string query = "select idProfesores, nombre, apellido from profesores where idProfesores = " + idProfesor ;

        //        SqlCommand command = new SqlCommand();
        //        //command.Connection = connection;
        //        command.CommandText = query;
        //        command.CommandType = CommandType.Text;

        //        //Ejecuto query y lo cargo en un data reader
        //        dr = Ac.ExecuteReader(command);
                
        //        //dr = Ac.execDR(query);
                
        //        while (dr.Read())
        //        {
        //            profesor = dr[0].ToString() + "-" + dr[1].ToString() + " " + dr[2].ToString();
        //        }
                
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("Error al cargar Profesor" + ex.Message);
        //        //return null;
        //    }
        //    finally
        //    {
        //        dr.Close();
        //    }
        //    return profesor;
        //}

        //
        //private string obtenerMateria(int id)
        //{
        //    try
        //    {
        //        string query = null;

        //        System.Data.DataTable dt = new System.Data.DataTable();
        //        AccesoDB ac = new AccesoDB();
        //        query = "coop_materia_GetOne " + id.ToString();
                

        //        SqlCommand command = new SqlCommand();
        //        //command.Connection = connection;
        //        command.CommandText = query;
        //        command.CommandType = CommandType.Text;

        //        dt = ac.execDT(command);

        //        foreach (DataRow dr in dt.Rows)
        //        {
        //            return dr[0].ToString();
        //        }

                
        //    }
        //    catch
        //    { }
        //    return "";
        //}

        //private void cargarNombre()
        //{
        //    SqlDataReader dr = null;
        //    try
        //    {
        //        AccesoDB Ac = new AccesoDB();
        //        string query = "select idTipoCurso, Nombre from tipoCursos ";

        //        SqlCommand command = new SqlCommand();
        //        //command.Connection = connection;
        //        command.CommandText = query;
        //        command.CommandType = CommandType.Text;

        //        //Ejecuto query y lo cargo en un data reader
        //        dr = Ac.ExecuteReader(command);

        //        //dr = Ac.execDR(query);

        //        while (dr.Read())
        //        {
        //            this.comboNom.Items.Add(dr[0].ToString() + "-" + dr[1].ToString());
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("Error al cargar combo de Profesores" + ex.Message);
        //    }
        //    finally
        //    {
        //        dr.Close();
        //    }
        //}

        

        private void btnAgregAlum_Click(object sender, EventArgs e)
        {
            try
            {
                if ( this.maskedNroCur.Text.Trim() != "")
                {
                    int idCurso = Convert.ToInt32(this.maskedNroCur.Text.Trim());
                    string nomCurso = this.comboNom.Text.Trim();
                    string dia = this.comboDia.Text.Trim();
                    string horaIni = this.maskInicio.Text.Trim();
                    string horaFin = this.maskFin.Text.Trim();
                    string prof = this.comboProf.Text.Trim();

                    AgregarAlumno aa = new AgregarAlumno(idCurso, nomCurso, dia, horaIni, horaFin, this.inst);
                    aa.Show(this);
                }
                else
                {
                    MessageBox.Show("Debe seleccionar un curso", "Atencion");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Se produjo un error al querer abrir formulario de agregar alumno: " + ex.Message);
            }
        }

               
        private void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            
            
            int i = this.dataGridView1.CurrentRow.Index;
            if (i >= 0)
            {
                string Materia = dataGridView1.Rows[i].Cells[1].Value.ToString();
                this.comboNom.Text = Materia;

                string profesor = dataGridView1.Rows[i].Cells[2].Value.ToString();
                this.comboProf.Text = profesor;



            //    string nomCurso = dataGridView1.Rows[i].Cells[1].Value.ToString();
            //    this.comboNom.Text = nomCurso.Trim();

            //    string cupoLibre = dataGridView1.Rows[i].Cells[2].Value.ToString();
            //    this.maskCupLib.Text = cupoLibre.Trim();

            //    string fechaInicio = dataGridView1.Rows[i].Cells[3].Value.ToString();
            //    this.dateTimePicker1.Text = fechaInicio.Trim();

            //    string horaInicio = dataGridView1.Rows[i].Cells[4].Value.ToString();
            //    this.maskInicio.Text = horaInicio.Trim();

            //    string fechaFin = dataGridView1.Rows[i].Cells[5].Value.ToString();
            //    this.dateTimePicker2.Text = fechaFin.Trim();

            //    string horaFin = dataGridView1.Rows[i].Cells[6].Value.ToString();
            //    this.maskFin.Text = horaFin.Trim();

            //    string dia = dataGridView1.Rows[i].Cells[7].Value.ToString();
            //    this.comboDia.Text = dia.Trim();

            //    int idProfesor = Convert.ToInt32(dataGridView1.Rows[i].Cells[8].Value);
            //    string profesor = verProfesor(idProfesor);
            //    this.comboProf.Text = profesor.Trim();

            //    string aula = dataGridView1.Rows[i].Cells[10].Value.ToString();
            //    this.comboAula.Text = aula.Trim();

            //    string cupo = dataGridView1.Rows[i].Cells[11].Value.ToString();
            //    this.maskCupo.Text = cupo.Trim();

            //    string observacion = dataGridView1.Rows[i].Cells[12].Value.ToString();
            //    this.textObs.Text = observacion.Trim();

            //    //string localidad = dataGridView1.Rows[i].Cells[10].Value.ToString();
            //    //this.txtLocalidad.Text = localidad.Trim();

            //    //string email = dataGridView1.Rows[i].Cells[11].Value.ToString();
            //    //this.txtCorreo.Text = email.Trim();
            }
        }


        private void enlazarDataGridCursos()
        {
            try
            {
                //borro el enlace
                this.limpiarEnlaceDatos();
                //texto
                this.maskedNroCur.DataBindings.Add("Text", dataGridView1.DataSource, "id");
                this.maskCupo.DataBindings.Add("Text", dataGridView1.DataSource, "cupos");
                //this.maskCupLib.DataBindings.Add("Text", dataGridView1.DataSource, "cupos_libres");
                this.maskCosto.DataBindings.Add("Text", dataGridView1.DataSource, "costo");
                this.maskedTextBox2.DataBindings.Add("Text", dataGridView1.DataSource, "matricula");
                //this.textObs.DataBindings.Add("Text", dataGridView1.DataSource, "observacion");
                //Combo
                this.comboAula.DataBindings.Add("Text", dataGridView1.DataSource, "aula");
                //this.comboNom.DataBindings.Add("Text", dataGridView1.DataSource, "id_profesor");
                this.comboNom.DataBindings.Add("Text", dataGridView2.DataSource, "apellido");
                //this.comboProf.DataBindings.Add("Text", dataGridView1.DataSource, "id_materia");
                this.comboProf.DataBindings.Add("Text", dataGridView2.DataSource, "nombre");
                this.maskInicio.DataBindings.Add("Text", dataGridView1.DataSource, "hora_inicio");
                this.maskFin.DataBindings.Add("Text", dataGridView1.DataSource, "hora_fin");
                this.dateTimePicker1.DataBindings.Add("Text", dataGridView1.DataSource, "fecha_inicio");
                this.dateTimePicker2.DataBindings.Add("Text", dataGridView1.DataSource, "fecha_fin");
                this.comboDia.DataBindings.Add("Text", dataGridView1.DataSource, "dia");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error enlazando datos de cursos. " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void enlazarDataGridSeminario()
        {
            try
            {
                //borro el enlace
                this.limpiarEnlaceDatos();
                //texto
                
                this.maskedNroCur.DataBindings.Add("Text", dataGridView2.DataSource, "id");
                this.maskCupo.DataBindings.Add("Text", dataGridView2.DataSource, "cupos");
                //this.maskCupLib.DataBindings.Add("Text", dataGridView2.DataSource, "cupos_libres");
                this.maskCosto.DataBindings.Add("Text", dataGridView2.DataSource, "costo");
                //this.textObs.DataBindings.Add("Text", dataGridView2.DataSource, "observacion");
                //Combo
                this.comboAula.DataBindings.Add("Text", dataGridView2.DataSource, "aula");
                //this.comboNom.DataBindings.Add("Text", dataGridView2.DataSource, "id_profesor");
                this.comboNom.DataBindings.Add("Text", dataGridView2.DataSource, "apellido");
                //this.comboProf.DataBindings.Add("SelectedValue", dataGridView2.DataSource, "id_materia");
                this.comboProf.DataBindings.Add("SelectedValue", dataGridView2.DataSource, "nombre");
                this.maskInicio.DataBindings.Add("Text", dataGridView2.DataSource, "hora_inicio");
                this.maskFin.DataBindings.Add("Text", dataGridView2.DataSource, "hora_fin");
                this.dateTimePicker1.DataBindings.Add("Text", dataGridView2.DataSource, "fecha_inicio");
                this.dateTimePicker2.DataBindings.Add("Text", dataGridView2.DataSource, "fecha_fin");
                this.comboDia.DataBindings.Add("Text", dataGridView2.DataSource, "dia");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error enlazando datos de seminario. " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void limpiarEnlaceDatos()
        {
            try
            {
                //texto
                this.maskedNroCur.DataBindings.Clear();

                this.maskCupo.DataBindings.Clear();
                //this.maskCupLib.DataBindings.Clear();
                this.maskCosto.DataBindings.Clear();
                this.maskedTextBox2.DataBindings.Clear();
                this.textObs.DataBindings.Clear();
                //Combo
                this.comboAula.DataBindings.Clear();
                this.comboNom.DataBindings.Clear();
                this.comboProf.DataBindings.Clear();
                this.maskInicio.DataBindings.Clear();
                this.maskFin.DataBindings.Clear();
                this.dateTimePicker1.DataBindings.Clear();
                this.dateTimePicker2.DataBindings.Clear();
                this.comboDia.DataBindings.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error enlazando datos de seminario. " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        

        
        private void Cerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cargarData2();
            this.Cancelar.Enabled = true;
        }

        private void cargarData2()
        {
            string query = null;
            DataTable dt = new DataTable();
            AccesoDB ac = new AccesoDB();
            query = "SELECT idCurso, nombre, CuposLibres, fechaApertura, horaInicio, ";
            query += "fechaCierre, horaFin, dia, profesor, idTipoCurso, aula, cupo, observaciones "; 
            query += "FROM cursos where activo=1 order by idCurso ";

            SqlCommand command = new SqlCommand();
            //command.Connection = connection;
            command.CommandText = query;
            command.CommandType = CommandType.Text;

            dt = ac.execDT(command);

            //dt = ac.execDT(query);

            this.dataGridView1.DataSource = dt;

        }

        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    this.cursosTableAdapter.FillBy(this.escuelaDataSet5.cursos);
            //}
            //catch (System.Exception ex)
            //{
            //    System.Windows.Forms.MessageBox.Show(ex.Message);
            //}

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                this.enlazarDataGridCursos();
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton2.Checked)
            {
                this.enlazarDataGridSeminario();
            }
        }

        private void verClasesbutton2_Click(object sender, EventArgs e)
        {
            try
            {
                int idCurso = Convert.ToInt32(this.maskedNroCur.Text.Trim());
                VerClasesForm am = new VerClasesForm(this.inst, idCurso);
                am.Show(this);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error abriendo formulario. " + ex.Message, "Error");
            }
           
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = this.dataGridView2.CurrentRow.Index;
            if (i >= 0)
            {
                string Materia = dataGridView2.Rows[i].Cells[1].Value.ToString();
                this.comboNom.Text = Materia;

                string profesor = dataGridView2.Rows[i].Cells[2].Value.ToString();
                this.comboProf.Text = profesor;
            }
        }


        

     

       

        

      

    }
}